<?php
require_once dirname(__FILE__) . '/Migrate.abstract.php';
class WFMigrate20to20 extends AMigrate
{
    /**
     *\brief Desciption
     * This method is main() function of Migration WF20 to WF20
     * @see AMigrate::execute()
     */
    public function execute(){
        $strPathPidFile = $this->objWidgetPoolFactory->getWFHome() . '/repository/migrate20to20.pid';
        $objMutex = $this->objWidgetPoolFactory->getIOFactory()->getMutex($strPathPidFile);
        //Step1: Start checking is Migrate WF2.0 to WF2.0 process is mutex!
        if(true === $objMutex->isRunning()){
            $msg = __CLASS__ . '::' . __FUNCTION__ . '()=>Another Update process is running!It is not allowed concurrent Migrate WF2.0 to WF2.0 proccess!';
            throw new WFMigrateMigrateException($msg, WFOpt::UPGRADE_WF_FAILED_IS_NOT_MUTEX);
        }
        $msg = '>>>>Step '.$this->getStepNoAccumulate().': Success checking is Migrate WF2.0 to WF2.0 process is mutex!';
        $this->objLogger->info($msg);
        //Step2: update PATH info /path/to/repository/widgetPool/wpCurrent to /path/to/repository/widgetPool/WPInfo.ini
        try{
            $this->updateWPInfoIni();
        }catch(Exception $e){
            $strMsg = $strMsg = __CLASS__ . '::' . __FUNCTION__ . '()=>Failed in in Step2: update PATH info /path/to/repository/widgetPool/wpCurrent to /path/to/repository/widgetPool/WPInfo.ini => ' . $e->getMessage();
            throw new WFMigrateMigrateException($strMsg, WFOpt::UPGRADE_WF_FAILED_UPDATE_WPINFO_INI);
        }
        $msg = '>>>>Step2: Success update PATH info /path/to/repository/widgetPool/wpCurrent to /path/to/repository/widgetPool/WPInfo.ini';
        $this->objLogger->info($msg);
        //Step3: Drop And Recreate TABLE serverlisttype
        try{
            $this->objFrameworkTo->getDbFactory()->getDB()->dropWFDBSchema('serverlisttype');
            $this->objFrameworkTo->getDbFactory()->getDB()->initWFDBSchema('serverlisttype');
        }catch(Exception $e){
            $strMsg = __CLASS__ . '::' . __FUNCTION__ . '()=> Failed in Step3: Drop And Recreate TABLE serverlisttype => ' . $e->getMessage();
            throw new WFMigrateMigrateException($strMsg, WFOpt::UPGRADE_WF_FAILED_DROP_AND_RECREATE_TABLE_SERVERLISTTYPE);
        }
        $msg = '>>>>Step3: Success Drop And Recreate TABLE serverlisttype';
        $this->objLogger->info($msg);
    }
    /**
     *\brief Description
     * - This method will return PATH of /path/to/widgetPool/WPInfo.ini
     * EX : /Users/jersus/htdocs/CruiseControl/projects/Widget_Framework/TMShare/AJAX-Widget/Dev/AJAX-Widgets-1.7/AJAX-Widgets/src/repository/widgetPool/WPInfo.ini
     * @return string PATH of /path/to/widgetPool/WPInfo.ini
     */
    public function getPathWPInfoIni(){
        return $this->objFrameworkTo->getDocumentRoot() . '/repository/widgetPool/WPInfo.ini';
    }
    /**
     *\brief Description:
     * - Change WPInfo
     * - Which define in /path/to/widgetPool/WPInfo.php
     */
    public function updateWPInfoIni($intLogId = null){
        $strPathWPInfoIni = $this->getPathWPInfoIni();
        $strPathLogIdHaveToReplace = $intLogId == null ? $this->objFrameworkTo->getWidgetPoolFactory()->getWPWebrootNoSlash() : 'repository/widgetPool/wp' . $intLogId;
        $contentWPInfoIni = '[PATH]
CurrentWidgetPoolPath = ' . $strPathLogIdHaveToReplace;
        $isSuccess = $this->objFrameworkTo->getIOFactory()->getFile()->writeDataSafe($strPathWPInfoIni, 'w+', $contentWPInfoIni);
        return $isSuccess;
    }
    /*protected function getArrsObjDataMigrate(){
        return array();
    }*/
    protected function initColDataMigrate(){
        //do nothing
    }
}